package com.frogs.ucenter.controller;

import com.frogs.framework.base.web.BaseController3;
import com.frogs.framework.util.DWZResponse;
import com.frogs.framework.util.PageList;
import com.frogs.framework.util.RenderUtil;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.system.util.TaskActionStatusEnum;
import com.frogs.ucenter.entity.ActionInfo;
import com.frogs.ucenter.service.ActionInfoService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/admin/actionInfo")
public class ActionInfoController extends BaseController3 {
    private static final String ROOT_VIEW = "admin/actionInfo";

    private static final String HOME_VIEW = "admin/actionInfo/home";

    private static final String EDIT_VIEW = "admin/actionInfo/edit";

    @Autowired
    private ActionInfoService actionInfoService;

    @RequestMapping()
    public String listActionInfo(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, true );
        getPageList( model );
        return HOME_VIEW;
    }

    @RequestMapping(EDIT)
    public String createActionInfo(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, false );
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            model.put( "model",actionInfoService.getById( id ) ); 
        }
        return EDIT_VIEW;
    }

    @RequestMapping(ADD)
    public String saveActionInfo(HttpServletRequest request, HttpServletResponse response, ModelMap model, ActionInfo actionInfo) {
        setRequestModelMap(request, model, false);
        DWZResponse.Builder builder;
        try {
            actionInfoService.insert( actionInfo ); 
            builder = DWZResponse.getSucessBuilder("新增成功");
        } catch (Exception e) {
            builder = DWZResponse.getFailBuilder("新增失败" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        return null;
    }

    @RequestMapping(SHOW)
    public String showActionInfo(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            model.addAttribute( actionInfoService.getById( id ) ); 
        }
        setRequestModelMap(request, model);
        return null;
    }

    @RequestMapping(UPDATE)
    public String updateActionInfo(HttpServletRequest request, HttpServletResponse response, ModelMap model, ActionInfo actionInfoNew) throws Exception {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            DWZResponse.Builder builder;
            try {
                ActionInfo actionInfo = actionInfoService.getById( id ); 
                PropertyUtils.copyProperties(actionInfo, actionInfoNew);
                actionInfoService.update( actionInfo ); 
                model.clear();
                builder = DWZResponse.getSucessBuilder("更新成功");
            } catch (Exception e) {
                builder = DWZResponse.getFailBuilder("更新失败" + e.getMessage());
            }
            RenderUtil.renderHtml(builder.build().toString(), response);
        }
        return null;
    }

    @RequestMapping(DELETE)
    public String deleteActionInfo(HttpServletRequest request, HttpServletResponse response, ModelMap model, @RequestParam(required=true, value="ids") String inIdList) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap(request, model);
        DWZResponse.Builder builder;
        try {
            String[] idArray = inIdList.split(",");
            String[] delArray = new String[idArray.length];
            int flag = 0;//判断是否存在已启用的动作，存在已启用的动作，则不能进行删除操作
            for(int i=0;i<idArray.length;i++){
            	if(Integer.parseInt(idArray[i].split(";")[1]) == TaskActionStatusEnum.ENABLED.getId()){
            		 flag = 1;
            	};
            	delArray[i] = idArray[i].split(";")[0];
            }
            if(flag == 0){
                model.put("inIdList", delArray);
                actionInfoService.delete( model ); 
                builder = DWZResponse.getSucessBuilder("删除成功");
            }else{
            	builder = DWZResponse.getFailBuilder("删除失败,已启用的动作不能删除");
            }

        } catch (Exception e) {
            builder = DWZResponse.getFailBuilder("删除失败" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        model.clear();
        return null;
    }

    private PageList<ActionInfo> getPageList(ModelMap model) {
        PageList<ActionInfo> actionInfoList = actionInfoService.getList( model, Integer.parseInt(model.get("pageNum").toString()), Integer.parseInt(model.get("numPerPage").toString()) ); 
        model.put("actionInfoList",actionInfoList);
        model.put("pageTurn",actionInfoList.getPageTurn());
        return actionInfoList;
    }
}